package ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.DefaultListModel;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import model.Entry;

public class MainPanel extends JPanel {

	private static final long serialVersionUID = 8920997326735022832L;

	// Listen und Panels.
	private JList itemList;
	private JPanel infoPanel;
	
	private JTextField nameField;
	private JTextField passwordField;
	
	public MainPanel() {
		super();
		
		this.setLayout(new BorderLayout());
		
		// InfoPanel.
		SpringLayout layout = new SpringLayout();
		infoPanel = new JPanel(layout);
		
		JLabel nameLabel = new JLabel("Name:");
		nameField = new JTextField();
		nameField.setPreferredSize(new Dimension(200, 30));
		
		JLabel passwordLabel = new JLabel("Passwort:");
		passwordField = new JTextField();
		passwordField.setPreferredSize(new Dimension(200, 30));
		
		infoPanel.add(nameLabel);
		infoPanel.add(nameField);
		infoPanel.add(passwordLabel);
		infoPanel.add(passwordField);
		
		layout.putConstraint(SpringLayout.WEST, nameLabel, 15, SpringLayout.WEST, infoPanel);
		layout.putConstraint(SpringLayout.NORTH, nameLabel, 15, SpringLayout.NORTH, infoPanel);
		
		layout.putConstraint(SpringLayout.WEST, nameField, 0, SpringLayout.WEST, nameLabel);
		layout.putConstraint(SpringLayout.NORTH, nameField, 5, SpringLayout.SOUTH, nameLabel);
		
		layout.putConstraint(SpringLayout.WEST, passwordLabel, 0, SpringLayout.WEST, nameLabel);
		layout.putConstraint(SpringLayout.NORTH, passwordLabel, 15, SpringLayout.SOUTH, nameField);
		
		layout.putConstraint(SpringLayout.WEST, passwordField, 0, SpringLayout.WEST, nameLabel);
		layout.putConstraint(SpringLayout.NORTH, passwordField, 5, SpringLayout.SOUTH, passwordLabel);
		
		// Liste.
		DefaultListModel dlm = new DefaultListModel();
		dlm.addElement(new Entry("Pippi", "hallo_", "hallo"));
		dlm.addElement(new Entry("Kakka", "hallo_", "hallo1"));
		dlm.addElement(new Entry("Puupu", "hallo_", "hallo2"));
		
		itemList = new ItemList(this, dlm);
		itemList.setPreferredSize(new Dimension(230, 0));
		itemList.setCellRenderer(new ListItemRenderer());
		itemList.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
		
		this.add(new JScrollPane(itemList), BorderLayout.WEST);
		this.add(infoPanel, BorderLayout.CENTER);
	}
	
	public void showEntry(Entry e) {
		nameField.setText(e.getName());
		passwordField.setText(e.getPassword());
	}
}
